/**
 * 获取数据类型
 * @param obj
 * @returns {*}
 */
export function typeOf(obj: any): 'boolean' | 'number' | 'string' | 'function' | 'array' | 'date' | 'regExp' | 'undefined' | 'null' | 'object' {
    const toString = Object.prototype.toString;
    const map: { [key: string]: 'boolean' | 'number' | 'string' | 'function' | 'array' | 'date' | 'regExp' | 'undefined' | 'null' | 'object' } = {
        '[object Boolean]': 'boolean',
        '[object Number]': 'number',
        '[object String]': 'string',
        '[object Function]': 'function',
        '[object Array]': 'array',
        '[object Date]': 'date',
        '[object RegExp]': 'regExp',
        '[object Undefined]': 'undefined',
        '[object Null]': 'null',
        '[object Object]': 'object'
    };
    return map[toString.call(obj)];
}


/**
 * 判断基础类型数据是否为空
 * @param value
 */
export function isEmpty(value: any) {
    switch (typeOf(value)) {
        case "array":
            return value.length === 0
        case "object":
            return true
        case "string":
            return value.length === 0
        case "number":
            return false
    }
    return true
}


/**
 * 克隆数据防止引用
 * @param data
 * @returns {{}|*}
 */
export function CloneData(data: any): any {
    const type: string = typeOf(data);
    if (type === 'array') {
        let cloneData: Array<any> = [];
        for (let i = 0; i < data.length; i++) {
            cloneData.push(CloneData(data[i]));
        }
        return cloneData
    } else if (type === 'object') {
        let cloneData: { [key: string]: any } = {};
        for (let i in data) {
            cloneData[i] = CloneData(data[i]);
        }
        return cloneData
    } else {
        return data;
    }
}


/**
 * 获取JSON值
 */
export function getJsonValue(obj: { [key: string]: any }, property: string | string[], def: any = null): any {
    let keys = property instanceof Array ? property : property.split('.')
    let value: any = obj
    for (let key of keys) {
        if (typeof value[key] === 'undefined') {
            return def
        }
        value = value[key]
    }
    return value
}


/**
 * 获取原始数据
 * @param data
 */
export function getRawData(data: any) {
    if (typeOf(data) === 'object' && Reflect.has(data, '__v_isRef')) {
        return data.value
    }
    return data
}